package com.ndpmedia.fb.pmd.configration.db;

import com.ndp.fb.util.DateUtil;
import com.ndpmedia.fb.pmd.configration.model.po.AppPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.*;

/**
 * Created by Administrator on 2014/10/120.
 * modify by elvis.xiong
 */
@Repository
public class AppRepository extends BasePepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    public boolean findByName(String appName){

        String sql = "select * from app  where name='" + appName + "'";
        List<Map<String,Object>> result = this.jdbcTemplate.queryForList(sql);
        if( result.size()>0){
            return true;
        }
        return false;
    }

    public List<AppPo> findAll(String key) {
        String sql = "select * from app";
        if(key!=null&&key.length()>0)
            sql+=" where NAME like  '%"+key+"%'";
        return queryForListObject(sql, AppPo.class);
    }

    public AppPo save(final String name) {
        final String appid = name;
        Map params = new HashMap();
        params.put("appid", appid);
        params.put("name", name);
        final String sql = "INSERT INTO app(APPID, NAME,UPDATE_TIME) VALUES(?,?,?)";
        getLogInfo(sql, params);

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, appid);
                ps.setString(2, name);
                ps.setString(3, DateUtil.getCurrDate(DateUtil.FORMAT_ONE));
                return ps;
            }
        }, keyHolder);

        Integer id = keyHolder.getKey().intValue();
        AppPo app = new AppPo(id,appid,name);
        return app;
    }

    public boolean delete(String appid) {
        Map params = new HashMap();
        params.put("appid", appid);
        String sql = "delete from app where appid=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, appid);
        return true;
    }

    public boolean update(String appid,String name){
        Map params = new HashMap();
        params.put("appid", appid);
        params.put("name", name);
        String sql = "update app set name=? where appid=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, name,appid);
        AppPo app = new AppPo(0,appid,name);
        return true;
    }

    public boolean updateStatus(String appid, String status) {
        Map params = new HashMap();
        params.put("appid", appid);
        params.put("status", status);
        String sql = "update app set status=? where appid=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, status,appid);
        AppPo app = new AppPo(0,appid,"");
        return true;
    }
}
